package qdu.edu.com.fushanf4.service.count;

import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;

import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 用来过滤无效字段并，处理文本使每个用户类型字符串占一行，本地进行计数
 * 输入：LongWritable为行号，Text为一行的内容
 * 输出：Text为用户类型，IntWritable为出现次数
 */
public class UserTypeFrequencyMapper extends Mapper<LongWritable, Text, Text, IntWritable> {
    private final static IntWritable VALUE_OUT = new IntWritable(1);
    private final Text keyOut = new Text();
    private final static String REGEX = "[0-9.;；]";

    @Override
    protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
        // 读取一行
        String line = value.toString();

        // 处理
        // 切除第一列
        String[] split = line.split("\t");
        if (split.length <= 1) {    // 防止不干净的数据导致数组越界，有些数据可能并不符合一本性的规则，导致出人意料的结果
            return;
        }
        // 过滤无效字符
        // 匹配正则表达式
        Pattern pattern = Pattern.compile(REGEX);
        Matcher matcher = pattern.matcher(split[1]);

        // 用空字符串替换与正则表达式匹配的字符串
        line = matcher.replaceAll("");

        // 以“:”再次进行切片
        String[] userTypes = line.split(":");

        // 输出
        for (String userType : userTypes) {
            keyOut.set(userType);
            context.write(keyOut, VALUE_OUT);
        }
    }
}
